package com.me.blood.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.me.blood.R;
import com.me.blood.databinding.ActivityMainBinding;
import com.me.blood.frag.BaseTabFragment;
import com.me.blood.frag.HomeFragment;
import com.me.blood.frag.MeFragment;
import com.me.blood.frag.PressFragment;
import com.me.ad.ADType;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    ActivityMainBinding binding;
    BaseTabFragment[] fragments = new BaseTabFragment[4];
    List<View> tabs = new ArrayList<>();
    int current = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.history.setVisibility(View.GONE);
        tabs.add(binding.home);
        tabs.add(binding.card);
        tabs.add(binding.history);
        tabs.add(binding.me);
        for (int i = 0; i < tabs.size(); i++) {
            int j = i;
            tabs.get(i).setOnClickListener(view -> changeTab(j));
        }
        fragments[0] = new HomeFragment();
        replaceFragment(R.id.frame, fragments[0]);
        tabs.get(0).setSelected(true);
    }

    @Override
    protected ADType getXpType() {
        return null;
    }

    @Override
    protected void loadAd() {

    }

    private void changeTab(int i) {
        if (current == i) {
            return;
        }
        if (fragments[i] == null) {
            if (i == 1) {
                fragments[1] = new PressFragment();
            } else if (i == 2) {

            } else {
                fragments[i] = new MeFragment();
            }
            if (fragments[i] != null) {
                addFragment(R.id.frame, fragments[i]);
            }
        } else {
            showFragment(fragments[i]);
        }
        for (int j = 0; j < tabs.size(); j++) {
            if (i == j) {
                tabs.get(j).setSelected(true);
            } else {
                tabs.get(j).setSelected(false);
                if (fragments[j] != null) {
                    hideFragment(fragments[j]);
                }
            }
        }
        current = i;
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }
}
